from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver import ActionChains
import time
driver=Chrome(executable_path="D:\\chromeDown\\chromedriver_win32\\chromedriver.exe")

#登陆超星尔雅(手机+短信验证码)
username="******"
password="******"
login_url="http://study.swpu.edu.cn/portal"
driver.get(login_url)
#点击校内登陆
try:
    driver.find_element_by_xpath("//div[@class='Loginbefore']/a[2]").click()
    driver.find_element_by_name("username").send_keys(username)
    driver.find_element_by_name("password").send_keys(password)
    #点击登陆
    driver.find_element_by_class_name("login").click()
except:
    driver.execute_script("alert('登陆失败!')")

#点击进入空间
try:
    driver.find_element_by_xpath("//div[@class='zaf_course']/p[@class='zaf_text']/a").click()
    driver.close()
    driver.switch_to.window(driver.window_handles[0])
    time.sleep(3)
    frame=driver.switch_to.frame("frame_content")
    a_list=driver.find_elements_by_xpath("//div[@class='Mcon1img httpsClass']/a[1]")
except Exception as e:
    print(str(e))
    #driver.execute_script("alert('进入空间失败!')")

#遍历我学的课
for a in a_list:
    a.click()
    driver.switch_to.window(driver.window_handles[1])

    while True:
        try:
            driver.find_element_by_xpath("//span[@class='icon']/em[@class='orange']/../following-sibling::span[1]/a").click()
        except Exception as e:
            break
        time.sleep(2)
        test_elem=driver.find_element_by_xpath("//div[@class='tabtags']/span[@title='章节测验']")
        video_elem=driver.find_element_by_xpath("//div[@class='tabtags']/span[@title='视频']")
        test_elem.click()
        driver.switch_to.frame("iframe")
        driver.switch_to.frame(driver.find_element_by_xpath("//div[contains(@class,'ans-attach-ct')]/iframe"))
        driver.switch_to.frame("frame_content")

        #等待提交作业
        #driver.execute_script("alert('请完成章节测验')")
        while True:
            time.sleep(5)
            try:
                driver.find_element_by_xpath("//h3/span[text()='已完成']")
            except Exception as e:
                pass
                #print(str(e))
            else:
                break

        #点击视频按钮
        try:
            driver.switch_to.default_content()
            video_elem.click()
            driver.switch_to.frame("iframe")
            driver.switch_to.frame(driver.find_element_by_xpath("//div[@class='ans-attach-ct']/iframe"))
            time.sleep(2)

            #视频操作
            vedio=driver.find_element_by_id("video_html5_api")
            #driver.get(vedio.get_attribute("src"))

            #with open("test.html", "w", encoding="UTF8") as f:
            #    f.write(driver.page_source)
            #开始播放
            driver.execute_script("return arguments[0].play()",vedio)
            time.sleep(5)
            #静音
            driver.execute_script("arguments[0].muted=true", vedio)
            #检测视频是否播放完
            duration = driver.execute_script("return arguments[0].duration", vedio)
            #print(duration)
            time.sleep(duration)
            while True:
                time.sleep(10)
                ended=driver.execute_script("return arguments[0].ended",vedio)
                #print(ended)
                if ended:
                    break
        except Exception as e:
            pass
            #driver.execute_script("alert('视频播放出错!')")
            #print(str(e))

        time.sleep(3)
        #回到课堂
        driver.switch_to.default_content()
        driver.find_element_by_xpath("//div[@class='goback']/a").click()


    time.sleep(2)
    driver.close()
    driver.switch_to.window(driver.window_handles[0])
    frame = driver.switch_to.frame("frame_content")
    #with open("test.html", "w", encoding="UTF8") as f:
    #    f.write(driver.page_source)
